 /*
  *@(#)AdTrackRequestImpl.java		8 Sept. 2000
  *
  * Copyright: MetaPlus Inc & Inquiry Management Systems Ltd.  2000
  *
  * This software is the proprietary information of MetaPlus Inc & 
  * Inquiry Management Systems Ltd.  
  * Use is subject to license terms.
  */
package com.ims.iaa;

import java.sql.*;
import java.rmi.RemoteException;

/**
  * This class <code>AdTrackRequestImpl</code> represents the ad search request.
  * It provides the remote wrapper for the LocalRequest delegatee. It implements 
  * the <code>AdTrackRequest</code> remote interface and can be manipulated 
  * across the net between diffrent JVM. The accessor and mutor methods are 
  * actually implementated by its local delegatee.
  *
  * @see AdTrackRequest
  *
  * @author Anthony Zhang
  *
  * @version 1.00, 25 Sept. 2000
  */
public class AdTrackRequestImpl 
	extends java.rmi.server.UnicastRemoteObject 
	implements AdTrackRequest {

	/** 
	  * This <code>LocalRequest</code> object is the data model of the
	  * remote request object. It is wrappered so it can be manipulated by 
	  * object located in remote JVM.
      *
	  * @see #AdTrackRequestImpl
	  * @see #getLocalRequest	  	  
	  */	
	private LocalRequest localRequest;
	
	/**
	  *
	  *
	  *
	  */
	public AdTrackRequestImpl(LocalRequest localReq,int registryPort) 
		throws java.rmi.RemoteException {
		localRequest = (LocalRequest)localReq.clone();
		if (IaaDebug.StoreDB)  {
			if (localRequest.getRequestId() == -1)
				localRequest.setSpiderDB(
					IaaUtil.getSpiderDBConnection(IaaDebug.ServerHost,IaaDebug.ServerPort));
			localRequest.storeAdRequest();
		}
	}
	
	public void setRequestParams(RequestParameters reqParams)  {
		localRequest.setRequestParams(reqParams);
	}
	public RequestParameters getRequestParams()  {
		return localRequest.getRequestParams();
	}
	public boolean equals(AdTrackRequest adReq) throws RemoteException {
		return localRequest.equals(adReq.getLocalRequest());
	}
	
	public LocalRequest getLocalRequest() throws RemoteException  {
		return (LocalRequest)localRequest.clone();
	}
	
	public void setUrl(String URL) throws  RemoteException {
		localRequest.setUrl(URL);
	}
	
	public String getUrl() throws  RemoteException {
		return localRequest.getUrl();
	}
	
	public void setDepth(int depth) throws  RemoteException {
		localRequest.setDepth(depth);
	}
	
	public int getDepth() throws  RemoteException {
		return localRequest.getDepth();	
	}
	
	public void setStartTime(Timestamp startTime) throws  RemoteException {
		localRequest.setStartTime(startTime);
	}
	
	public Timestamp getStartTime() throws  RemoteException {
		return localRequest.getStartTime();
	}

	public void setEndTime(Timestamp endTime) throws  RemoteException {
		localRequest.setEndTime(endTime);
		raiseFinishEvent();
	}
	
	public Timestamp getEndTime() throws  RemoteException {
		 return localRequest.getEndTime();
	}

	public void setSpiderName(String name) throws  RemoteException  {
		localRequest.setSpiderName(name);
	}

	public void setSearchState(int state) throws  RemoteException  {
		localRequest.setSearchState(state);
	}
	private void raiseFinishEvent()  {
		String sName = localRequest.getSpiderName() + "_AdTrackFinishEvent";
		Thread finishEventThread = new IaaThread(sName)  {
			public void run()  {
				if(IaaDebug.Debug)IaaUtil.log( "Request [" + localRequest + 
					"] informed the Request Control Center of finish status");		
				try {
				//++Revision by ShengMeng Bai at 2004-10-04
					//RequestCtl reqCenter = IaaUtil.getRequestCenter("localhost",1099);
					RequestCtl reqCenter = IaaUtil.getRequestCenter(IaaDebug.ServerHost, IaaDebug.ServerPort);
				//--Revision by ShengMeng Bai at 2004-10-04
					//serverName,serverPort);	
					if ( reqCenter == null) {
		    			IaaUtil.log( "Can not connect to the Request Contorl Center" );		
						return;
					}				
	       			reqCenter.finishAdRequest(AdTrackRequestImpl.this);
		   			IaaUtil.log( "Request ["+localRequest+"] informed the finish status");
		      	} catch ( Exception re ) {
		        	IaaUtil.log( "finishEventThread Exception:" + re );
	  			}
			} // end of run()
		} ; //end of anonymous class
		finishEventThread.start();	
	}	


}
